﻿using Factory.Common;
using Factory.MasterData.MeasureUnits.Dto;
using Factory.MasterData.MeasureUnits;
using Factory.RBAC.Permissions.Dto;
using Pipelines.Sockets.Unofficial.Arenas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.RBAC.Permissions
{
    public class PermissionsAppService:ApplicationService, IPermissionsAppService
    {
        /// <summary>
        /// 权限接口
        /// </summary>
        private readonly IRepository<Permission, int> _permissionrepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountrepository"></param>
        public PermissionsAppService(IRepository<Permission, int> permissionrepository)
        {
            _permissionrepository = permissionrepository;
        }


        /// <summary>
        /// 树形权限
        /// </summary>
        /// <returns></returns>
        public async Task<List<TreePermissionDto>> TreePermission()
        {
            var sponse = new List<TreePermissionDto>();

            var treePermission = await _permissionrepository.GetListAsync();
            var list = treePermission.Where(m => m.Pid == 0 && m.IsDel == 1);
            foreach (var item in list)
            {
                TreePermissionDto model = new TreePermissionDto();
                model.id = item.Id;
                model.title = item.PermissionName;
                SetTree(treePermission, model);
                sponse.Add(model);
            }

            return sponse;

        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="treePermission"></param>
        /// <param name="parent"></param>
        private void SetTree(List<Permission> treePermission, TreePermissionDto parent)
        {
            var list = treePermission.Where(m => m.Pid == parent.id&&m.IsDel==1);
            foreach (var item in list)
            {
                TreePermissionDto model = new TreePermissionDto();
                model.id = item.Id;
                model.title = item.PermissionName;
                parent.children.Add(model);
                SetTree(treePermission, model);
            }
        }



        /// <summary>
        /// 获取具体数据进行反填
        /// </summary>
        /// <returns></returns>
        public async Task<Permission> GetPermission(int id)
        {
            var permission =await _permissionrepository.FindAsync(id);
            return permission;
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddSon(Permission permission) 
        {
            var verify =await _permissionrepository.CountAsync(m => m.PermissionName == permission.PermissionName);
            if (verify>0)
            {
                return -1;
            }
             await _permissionrepository.InsertAsync(permission);
            return 1;
        }

        /// <summary>
        /// 添加同集权限
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddEquality(Permission permission)
        {
            var verify = await _permissionrepository.CountAsync(m => m.PermissionName == permission.PermissionName);
            if (verify > 0)
            {
                return -1;
            }
            await _permissionrepository.InsertAsync(permission);
            return 1;
        }


        /// <summary>
        /// 修改权限
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddUpdate(PermissionDto permission)
        {
            //判断是否重复
            var isDuplicate = await _permissionrepository.CountAsync(m=>m.PermissionName== permission.PermissionName &&m.Id!= permission.Id);
            if (isDuplicate >0)
            {
                return -1; // 返回特定的状态码以表示重复
            }

            //获取数据
            var measureUnit = AutoMapperExt.MapTo<Permission>(permission);
            _permissionrepository.UpdateAsync(measureUnit);
            return 1;
        }



    }
}
